{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f84d8421",
   "metadata": {},
   "source": [
    "# Agno + Strata Integration\n",
    "\n",
    "This tutorial demonstrates how to build AI agents using an Agno with Klavis Strata MCP servers for enhanced functionality."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f1de804",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "\n",
    "Before we begin, you'll need:\n",
    "\n",
    "- **OpenAI API key** - Get at [openai.com](https://openai.com/)\n",
    "- **Klavis API key** - Get at [klavis.ai](https://klavis.ai/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8b0ff58",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install the required packages\n",
    "%pip install -q klavis openai agno"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "339ae264",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import webbrowser\n",
    "import asyncio\n",
    "from klavis import Klavis\n",
    "from klavis.types import McpServerName, ToolFormat\n",
    "from agno.agent import Agent\n",
    "from agno.models.openai import OpenAIChat\n",
    "from agno.tools.mcp import MCPTools\n",
    "\n",
    "\n",
    "# Set environment variables\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"  # Replace with your actual OpenAI API key\n",
    "os.environ[\"KLAVIS_API_KEY\"] = \"YOUR_KLAVIS_API_KEY\"  # Replace with your actual Klavis API key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de942d95",
   "metadata": {},
   "source": [
    "## Step 1: Create Strata MCP Server\n",
    "\n",
    "Create a unified MCP server that combines multiple services (Gmail and Slack) for enhanced agent capabilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67aa5670",
   "metadata": {},
   "outputs": [],
   "source": [
    "klavis_client = Klavis(api_key=os.getenv(\"KLAVIS_API_KEY\"))\n",
    "\n",
    "# Create a Strata MCP server with Gmail and Slack integrations\n",
    "response = klavis_client.mcp_server.create_strata_server(\n",
    "    user_id=\"1234\",\n",
    "    servers=[McpServerName.GMAIL, McpServerName.SLACK],\n",
    ")\n",
    "\n",
    "print(f\"🚀 Strata MCP server created successfully!\")\n",
    "\n",
    "# Handle OAuth authorization if needed\n",
    "if response.oauth_urls:\n",
    "    for server_name, oauth_url in response.oauth_urls.items():\n",
    "        webbrowser.open(oauth_url)\n",
    "        print(f\"🔐 Opening OAuth authorization for {server_name}\")\n",
    "        input(f\"Press Enter after completing {server_name} OAuth authorization...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dfbdbe4",
   "metadata": {},
   "source": [
    "## Step 2: Create Agent Loop\n",
    "\n",
    "Implement an agentic loop that allows Agent to use MCP tools to complete tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "555950be",
   "metadata": {},
   "outputs": [],
   "source": [
    "async def agno_with_mcp_server(mcp_server_url: str, user_query: str):\n",
    "    \"\"\"Run an Agno agent with Klavis MCP server tools.\"\"\"\n",
    "\n",
    "    async with MCPTools(transport=\"streamable-http\", url=mcp_server_url) as mcp_tools:\n",
    "        agent = Agent(\n",
    "            model=OpenAIChat(\n",
    "                id=\"gpt-4o\",\n",
    "                api_key=os.getenv(\"OPENAI_API_KEY\")\n",
    "            ),\n",
    "            instructions=\"You are a helpful AI assistant.\",\n",
    "            tools=[mcp_tools],\n",
    "            markdown=True,\n",
    "        )\n",
    "\n",
    "        response = await agent.arun(user_query)\n",
    "        return response.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c4cadca",
   "metadata": {},
   "source": [
    "## Step 3: Run the  Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abc07bf4",
   "metadata": {},
   "outputs": [],
   "source": [
    "async def main():\n",
    "    result = await agno_with_mcp_server(\n",
    "        mcp_server_url=response.strata_server_url,\n",
    "        user_query=\"Check my latest 5 emails and summarize them in a Slack message to #general\"\n",
    "    )\n",
    "    print(f\"\\nFinal Response: {result}\")\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    asyncio.run(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "861e39c5",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "🎉 Congratulations! You've successfully created an Agno agent that can:\n",
    "\n",
    "1. **Read emails** using the Gmail MCP server\n",
    "2. **Send Slack messages** using the Slack MCP server\n",
    "3. **Coordinate multiple services** through Klavis Strata MCP integration\n",
    "\n",
    "This demonstrates the power of combining Agno's advanced reasoning capabilities with Klavis MCP servers for building sophisticated AI workflows that can interact with multiple external services seamlessly."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
